﻿<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

if (!defined('APP_IN')) exit('Access Denied');
//include ('page.php');
include(INC_DIR . 'api.func.php');

//引入短信类
include(INC_DIR . 'Sms_api.php');
//当前模块
$mod_name = '车源api';
//允许操作
$ac_arr = array(
    'check' => 'token验证',
    'login' => '用户登录',
    'register' => '用户注册',
    'regjudge' => '注册验证',
    'modifypwd' => '修改密码',
    'search' => '查询车源',
    'cars' => '车源详情',
    'comparcode' => '对比车源信息',
    'assesscar' => '评估',
    'subcars' => '提交发布车源信息',
    'collectcars' => '车源收藏列表',
    'carmode' => '提交车源收藏',
    'delcollect' => '取消车源收藏',
    'demcode' => '提交经销商收藏',
    'mydealer' => '返回我的经销商收藏',
    'deldealercollect' => '取消经销商收藏',
    'dealercars' => '二手车经销商车源',
    'dealertype' => '商家类型',
    'optionlist' => '搜索参数',
    'city' => '城市列表',
    'brand' => '一级品牌列表',
    'secondbrand' => '其他级品牌列表',
    'subass' => '提交评估信息',
    'upload' => '上传图片',
    'sellcars' => '置为已售出',
    'mypublishcars' => '我发布的车源列表',
    'updateversion' => '版本更新',
    'deletecars' => '删除二手车',
    'indexad' => '加载首页图片',
    'servicetel' => '客服电话',
    'help' => '攻略',
    'newstype' => '攻略详情',
    'details' => '内容详情',
    'subscribe' => '订阅相关',
    'sms' => '短信接口',
    'device' => '关联设备tokens',
    'Im' => '更新用户环信账号',
    'param' => '获取排放标准，变速',
    'avatar' => '更新头像',
    'userinfo' => '获取用户信息'
);

//当前操作
$ac = isset($_REQUEST['a']) && isset($ac_arr[$_REQUEST['a']]) ? $_REQUEST['a'] : '';

$func = array('get_User_Subscribe_NewNum', 'get_User_Subscribe_Rule_List', 'set_Subscribe_Rule_Newnum_Clean', 'add_Subscribe_Rule', 'update_Subscribe_Rule', 'delete_Subscribe_Rule');
//订阅相关操作操作参数
$f = isset($_REQUEST['f']) && in_array($_REQUEST['f'], $func) ? $_REQUEST['f'] : '';


//加载首页广告获取token
if ($ac == "indexad") {
    $appid = isset($_GET['appid']) ? $_GET['appid'] : '';
    $appkey = isset($_GET['appkey']) ? $_GET['appkey'] : '';
    echo indexad($appid, $appkey);
} else {
    $token = isset($_REQUEST['token']) ? trim($_REQUEST['token']) : '';
    //echo $lastoken = urldecode($token);
    $lastoken = decrypt('phpstats', $token);
    if (!empty($lastoken)) {
        $array_token = explode("#", $lastoken);
//        echo "<pre>";
//        var_dump($array_token);exit;
        $tokendata = $db->row_select_one('token', "id=1");
        $isintoken = strstr($lastoken, $tokendata['token']);
        if (empty($isintoken)) {
            $tokenlist = array('istoken' => 0);
            $jsonlist = json_encode($tokenlist);
            echo $jsonlist;
            exit;
        }
        //登录
        if ($ac == "login") {
            $username = isset($_POST['mobilephone']) ? trim($_POST['mobilephone']) : '';
            $password = isset($_POST['password']) ? md5(trim($_POST['password'])) : '';

            if (!empty($username) and !empty($password)) {
                echo user_login($username, $password);
            }
        } //商家类型
        elseif ($ac == "dealertype") {
            echo select_dealertype();
        } //判断用户名和手机号是否已注册过
        elseif ($ac == "regjudge") {
            $username = $_GET['username'];
            $mobilephone = $_GET['mobilephone'];
            if (!empty($username)) {
                echo user_register_username($username);
            } elseif (!empty($mobilephone)) {
                echo user_register_mobilephone($mobilephone);
            }
        } //注册
        elseif ($ac == "register") {
            $password = isset($_POST['password']) ? md5(trim($_POST['password'])) : '';
            $nicname = isset($_POST['nicname']) ? trim($_POST['nicname']) : '';
            $mobilephone = isset($_POST['mobilephone']) ? trim($_POST['mobilephone']) : '';
            $email = isset($_POST['email'])  ? trim($_POST['email']) : '';
            $company = isset($_POST['company']) ? trim($_POST['company']) : '';
            $aid = isset($_POST['aid']) ? trim($_POST['aid']) : '';
            $cid = isset($_POST['cid']) ? trim($_POST['cid']) : '';
            $code = isset($_POST['code']) ? trim($_POST['code']) : '';
            //$isIm = isset($_POST['isIm']) && in_array($_POST['isIm'], array(1, 2)) ? intval($_POST['isIm']) : 1;
            if (!empty($nicname) and !empty($password) and !empty($mobilephone)) {
                echo user_register($password, $nicname, $mobilephone, $email, $company, $aid, $cid, $code);
            }
        } //修改密码
        elseif ($ac == "modifypwd") {
            $uid = $array_token[0];
            $oldpwd = isset($_POST['oldpwd']) ? trim($_POST['oldpwd']) : '';
            $newpwd = isset($_POST['newpwd']) ? md5(trim($_POST['newpwd'])) : '';
            if (!empty($uid) and !empty($oldpwd) and !empty($newpwd)) {
                echo user_modifypwd($uid, $oldpwd, $newpwd);
            }
        } //查询用户信息
        elseif ($ac == "searchuser") {
            echo get_user($id);
        } //搜索参数
        elseif ($ac == "optionlist") {
            echo select_option();
        } //车源列表
        elseif ($ac == "search") {
            $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
            $status = isset($_GET['s']) ? intval($_GET['s']) : 0;
            $brand = isset($_GET['brand']) ? $_GET['brand'] : 0;;
            $subbrand = isset($_GET['subbrand']) ? intval($_GET['subbrand']) : 0;
            $price = isset($_GET['price']) ? intval($_GET['price']) : 0;
            $age = isset($_GET['age']) ? intval($_GET['age']) : 0;
            $kilometre = isset($_GET['kilometre']) ? intval($_GET['kilometre']) : 0;
            $transmission = isset($_GET['transmission']) ? intval($_GET['transmission']) : 0;
            $gas = isset($_GET['gas']) ? intval($_GET['gas']) : 0;
            $color = isset($_GET['color']) ? intval($_GET['color']) : 0;
            $source = isset($_GET['source']) ? intval($_GET['source']) : 0;
            $class = isset($_GET['class']) ? intval($_GET['class']) : 0;
            $picture = isset($_GET['picture']) ? intval($_GET['picture']) : 0;
            $keywords = isset($_GET['keywords']) ? $_GET['keywords'] : 0;;
            $mobilecode = isset($_REQUEST['mobilecode']) ? $_REQUEST['mobilecode'] : '';
            $identify = isset($_GET['identify']) ? intval($_GET['identify']) : 0;
            $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
            $tel = isset($_GET['tel']) ? intval($_GET['tel']) : 0;
            $code = isset($_REQUEST['code']) ? $_REQUEST['code'] : '';
            $aid = isset($_GET['aid']) ? intval($_GET['aid']) : 0;
            $cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            $sameprice = isset($_GET['sameprice']) ? intval($_GET['sameprice']) : 0;
            $samebrand = isset($_GET['samebrand']) ? intval($_GET['samebrand']) : 0;
            $comparcode = isset($_GET['comparcode']) ? intval($_GET['comparcode']) : 0;
            echo search_cars($status, $page, $brand, $subbrand, $price, $age, $kilometre, $transmission, $gas, $color, $source, $class, $picture, $aid, $cid, $uid, $keywords, $mobilecode, $identify, $tel, $code, $sameprice, $samebrand, $comparcode);
//            echo "<pre>";
//            var_dump(json_decode(search_cars($status, $page, $brand, $subbrand, $price, $age, $kilometre, $transmission, $gas, $color, $source, $class, $picture, $aid, $cid, $uid, $keywords, $mobilecode, $identify, $tel, $code, $sameprice, $samebrand, $comparcode)));
        } //车源收藏列表
        elseif ($ac == "collectcars") {
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            $orderby = isset($_GET['s']) ? intval($_GET['s']) : 0;
            echo collect_cars($uid, $orderby);
        } //我发布的车源列表
        elseif ($ac == "mypublishcars") {
            $uid = intval($array_token[0]);
            if (strlen($array_token[0]) > 10) {
                $mobilecode = trim($array_token[0]);
                $uid = 0;
            } else {
                $mobilecode = "";
                $uid = intval($array_token[0]);
            }
            $orderby = isset($_GET['orderby']) ? intval($_GET['orderby']) : 0;
            echo my_publishcars($uid, $mobilecode, $orderby);
        } //车源详情
        elseif ($ac == "cars") {
            $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            //$mobilecode = isset($_GET['mobilecode']) ? intval($_GET['mobilecode']) : 0;
            echo search_cars_detail($id, $uid);
        } //提交车源信息
        elseif ($ac == "subcars") {
            $uid = intval($array_token[0]);
            if (strlen($array_token[0]) > 10) {
                $mobilecode = trim($array_token[0]);
                $uid = 0;
            } else {
                $mobilecode = "";
                $uid = intval($array_token[0]);
            }
            $ios = intval($_POST['ios']);
            if ($ios == 1) {
                $mobilecode = trim($_POST['mobilecode']);
            }
            $carid = $array_token[2];
            $cid = $_POST['cid'];
            $aid = $_POST['aid'];
            $brand = $_POST['brand'];
            $subbrand = $_POST['subbrand'];
            $picone = $_POST['picone'];
            $pictwo = $_POST['pictwo'];
            $picthree = $_POST['picthree'];
            $picfour = $_POST['picfour'];
            $picfive = $_POST['picfive'];
            $picsix = $_POST['picsix'];
            $picseven = $_POST['picseven'];
            $piceight = $_POST['piceight'];
            $picnine = $_POST['picnine'];
            $color = $_POST['color'];
            $price = $_POST['price'];
            $details = $_POST['details'];
            $carname = $_POST['carname'];
            $model = $_POST['model'];
            $tel = $_POST['tel'];
            $allname = $_POST['allname'];
            $subsubbrand = $_POST['subsubbrand'];
            $sex = $_POST['sex'];
            $username = $_POST['username'];
            $ios = $_POST['ios'];
            $year = $_POST['year'];
            $month = $_POST['month'];
            $surveydate = $_POST['surveydate'];
            $insurancedate = $_POST['insurancedate'];
            $kilometre = $_POST['kilometre'];
            $transmission = $_POST['transmission'];
            $gas = $_POST['gas'];
            $emission = $_POST['emission'];
            echo submit_cars($brand, $subbrand, $subsubbrand, $allname, $model, $emission, $gas, $year, $month, $surveydate, $insurancedate, $aid, $cid, $kilometre, $transmission, $color, $price, $details, $picone, $pictwo, $picthree, $picfour, $picfive, $picsix, $picseven, $piceight, $picnine, $username, $tel, $mobilecode, $uid, $ios, $carid);
        } //上传图片(ios)
        elseif ($ac == "upload") {
            $mobilecode = $_POST['mobilecode'];
            echo submit_upload($mobilecode);
        } //一级品牌列表
        elseif ($ac == "brand") {
            echo select_brands($brand);
        } //其他级品牌列表
        elseif ($ac == "secondbrand") {
            $brand = isset($_GET['brand']) ? $_GET['brand'] : 0;
            echo select_secondbrands($brand);
        } //城市列表
        elseif ($ac == "city") {
            echo select_city();
        } //返回我的咨询信息
        elseif ($ac == "consult") {
            $mobilecode = $_REQUEST['mobilecode'];
            $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
            echo select_consult($mobilecode);
        } //返回我的评估师信息
        elseif ($ac == "appraiser") {
            echo select_appraiser();
        } //返回车源对比信息
        elseif ($ac == "compcar") {
            $ids = isset($_GET['ids']) ? $_GET['ids'] : 0;
            echo select_compcar($ids);
        } //返回我的评估车源信息
        elseif ($ac == "asscars") {
            $mobilecode = isset($_GET['mobilecode']) ? intval($_GET['mobilecode']) : 0;
            $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
            echo select_ascars($id, $mobilecode);
        } //提交评估车源信息
        elseif ($ac == "subass") {
            $mobilecode = $_POST['mobilecode'];
            $brand = isset($_POST['brand']) ? intval($_POST['brand']) : 0;
            $subbrand = isset($_POST['subbrand']) ? intval($_POST['subbrand']) : 0;
            $subsubbrand = isset($_POST['subsubbrand']) ? intval($_POST['subsubbrand']) : 0;
            $aid = isset($_POST['aid']) ? intval($_POST['aid']) : 0;
            $cid = isset($_POST['cid']) ? intval($_POST['cid']) : 0;
            $bid = isset($_POST['bid']) ? intval($_POST['bid']) : 0;
            $kilometre = isset($_POST['kilometre']) ? intval($_POST['kilometre']) : 0;
            $allname = $_POST['allname'];
            $year = $_POST['year'];
            $month = $_POST['month'];
            $tel = $_POST['tel'];
            $details = $_POST['details'];
            $question = $_POST['question'];
            $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
            echo submit_assess($mobilecode, $brand, $subbrand, $subsubbrand, $allname, $year, $month, $kilometre, $tel, $details, $aid, $cid, $question, $id);
        } //提交车源收藏
        elseif ($ac == "carmode") {
            $uid = intval($array_token[0]);
            $carid = intval($array_token[2]);
            echo insert_mycarsmobilecode($uid, $carid);
        } //批量删除车源收藏
        elseif ($ac == "delcollect") {
            $uid = intval($array_token[0]);
            $carid = intval($array_token[2]);
            echo del_mycarsmobilecode($uid, $carid);
        } //提交经销商收藏
        elseif ($ac == "demcode") {
            $uid = isset($_POST['uid']) ? intval($_POST['uid']) : 0;
            $dealerid = isset($_POST['dealerid']) ? intval($_POST['dealerid']) : 0;
//			$uid = intval($array_token[0]);
//			$dealerid = intval($array_token[2]);
            echo insert_mydealermobilecode($uid, $dealerid);
        } //批量删除经销商收藏
        elseif ($ac == "deldealercollect") {
            $uid = intval($array_token[0]);
            $dealerid = intval($array_token[2]);
            echo del_mydealermobilecode($uid, $dealerid);
        } //返回我的经销商收藏
        elseif ($ac == "mydealer") {
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            echo select_dealer($uid);
        } //二手车经销商车源
        elseif ($ac == "dealercars") {
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            $status = isset($_GET['status']) ? intval($_GET['status']) : 0;
            $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
            echo dealer_cars($uid, $status, $page);
        } //返回更新版本信息
        elseif ($ac == "updateversion") {
            $ios = isset($_GET['ios']) ? intval($_GET['ios']) : 0;
            echo update_version($ios);
        } //评估
        elseif ($ac == "assesscar") {
            $mk = isset($_GET['mk']) ? intval($_GET['mk']) : 0; //跑了多少公里
            $smk = isset($_GET['smk']) ? intval($_GET['smk']) : 30; //车辆限制一共多少公里 一般为30万公里
            $reyear = isset($_GET['reyear']) ? intval($_GET['reyear']) : 0; //使用多少年
            $remonth = isset($_GET['remonth']) ? intval($_GET['remonth']) : 0; //使用多少月
            $subsubbrand = isset($_GET['subsubbrand']) ? intval($_GET['subsubbrand']) : 0;
            echo tel_rapidasscars($subsubbrand, $mk, $reyear, $remonth);
        } //删除二手车
        elseif ($ac == 'deletecars') {
            if (strlen($array_token[0]) > 10) {
                $mobilecode = trim($array_token[0]);
                $uid = 0;
            } else {
                $mobilecode = "";
                $uid = intval($array_token[0]);
            }
            $carid = intval($array_token[2]);
            echo deletecars($carid, $uid, $mobilecode);
        } //置为已售出
        elseif ($ac == "sellcars") {
            if (strlen($array_token[0]) > 10) {
                $mobilecode = trim($array_token[0]);
                $uid = 0;
            } else {
                $mobilecode = "";
                $uid = intval($array_token[0]);
            }
            $carid = isset($_POST['carid']) ? intval($_POST['carid']) : 0;
            $uid = isset($_POST['uid']) ? intval($_POST['uid']) : 0;
            echo sellcars($carid, $uid, $mobilecode);
        } //客服电话
        elseif ($ac == "servicetel") {
            $list = array(
                'tel' => array(
                    '0' => '4000-797-555',
                    '1' => '4000-797-555'
                )
            );
            $jsonlist = json_encode($list);
            echo $jsonlist;
        } //攻略列表
        elseif ($ac == "help") {
//            $list = array(
//                'type'=> array(
//                    '1'=>'二手车行情',
//                    '2'=>'交易过户',
//                    '3'=>'汽车养护',
//                    '4'=>'驾驶技巧',
//                    '5'=>'测试'
//                )
//            );
//            $jsonlist = json_encode($list);
            $jsonlist = get_news();
            echo $jsonlist;
        } //攻略详情
        elseif ($ac == "newstype") {
            $helpid = isset($_GET['helpid']) ? intval($_GET['helpid']) : 1;
            $type = isset($_GET['type']) ? intval($_GET['type']) : 1;
            $jsonlist = get_helplist($helpid, $type);

            //var_dump(json_decode($jsonlist));exit;
            echo $jsonlist;
        } //内容详情
        elseif ($ac == "details") {
            $typeid = isset($_GET['typeid']) ? intval($_GET['typeid']) : 1;
            $jsonlist = get_typelist($typeid);

            echo $jsonlist;
        } //订阅相关操作接口
        elseif ($ac == "subscribe") {
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            if (!empty($f) and !empty($uid)) {
                echo call_user_func($f, $uid);
            }
            exit;

        } elseif ($ac == "sms") { //短信接口
            $st = (isset($_GET['status']) && in_array($_GET['status'], array(1, 2))) ? $_GET['status'] : 1;
            switch ($st) {
                case 1:
                    echo sms_send($_GET['mobilephone'], $_GET['type']);
                    //var_dump(json_decode(sms_send($_GET['mobilephone'], $_GET['type'])));
                    break;
                case 2:
                    echo check_sms($_GET['mobilephone'], $_GET['code'], $_GET['type']);
                    break;
            }

        } elseif ($ac == "device") {
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            $tokens = isset($_GET['tokens']) ? intval($_GET['tokens']) : 0;
            if ($uid && $tokens) {
                echo update_user_device_tokens($uid, $tokens);
            }
        } elseif ($ac == "Im") {
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            update_user_Im($uid);
        } elseif ($ac == "param") {
            $key = (isset($_GET['status']) && in_array($_GET['status'], array(1, 2))) ? $_GET['status'] : 1;
            $array = array();
            switch ($key) {
                case 1://排放标准
                    foreach ($commoncache['emstandardlist'] as $v) {
                        $array[] = $v;
                    }
                    echo json_encode($array);
                    break;
                case 2://变速箱
                    foreach ($commoncache['transmissionlist'] as $v) {
                        $array[] = $v;
                    }
                    echo json_encode($array);
                    break;
            }
            unset($array);
        } elseif ($ac == 'avatar') {
            $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
            if ($uid) update_user_avatar($uid);

        } elseif ($ac == 'userinfo') {
            $tel = isset($_GET['tel']) ? trim($_GET['tel']) : 0;
            echo get_user_info($tel);
        } else {
            $tokenlist = array('istoken' => 0);
            $jsonlist = json_encode($tokenlist);
            echo $jsonlist;
            exit;
        }
    }
}
?>
